using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MainGame : MonoBehaviour {
	
	public GUISkin 	   mainSkin;
	
	public AudioResource audio;
	
	public GameObject start_title;
	public GameObject start_playButton;
	
	public GameObject meet_title;
	public MeetUI	  meet_ui;
	
	public GameObject mate_title;
	public MatingUI   mate_ui;
	private int 	  mateDifficulty=0;
	private int 	  generation=1;
	
	
	public GameObject fight_title;
	public FightingUI fight_ui;
	private int 	  fightDifficulty=0;
	private int 	  fightEnemy=0;

	public GameObject final_title;
	public FinalUI    final_ui;
	
	public int 	  playerWins=0;
	public int 	  playerLoses=0;
	
	public AlienGenome playerAlien;
	public List<AlienGenome> enemyAliens= new List<AlienGenome>();
	
	private enum STATE{ START_SCREEN, MEET, MATE, FIGHT_PRE, FIGHT, CREDITS };
	private STATE state= STATE.START_SCREEN;
	
	bool once=true;
	void Start() {
		
		if(once) {
			meet_ui.done+= this.meet_Done;
			mate_ui.done+= this.mate_Done;
			mate_ui.confirmOffspring+= this.mate_OffspringConfirmed;
			fight_ui.done= this.fight_Done;
			fight_ui.finished= this.fight_Finished;
		}

		gotoStartScreen();	
	}
	
	void Update () {
		if( state == STATE.START_SCREEN ) {
			updateStart();	
		}
	}
	
	void OnGUI() {
		GUI.skin= mainSkin;	
	}
	
	void updateStart() {
		if(Input.GetMouseButtonDown(0))
		{
			Ray ray= Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit= new RaycastHit();
			if( start_playButton.collider.Raycast(ray, out hit, 50) )
			{
				leaveStartScreen();
				gotoMeet();
			}
		}	
	}
	
	void gotoStartScreen() {
		state= STATE.START_SCREEN;
		audio.play( AudioResource.STATE.MAIN_THEME );
		start_title.SetActiveRecursively(true);
	}
	
	void leaveStartScreen() {
		if(state == STATE.START_SCREEN ){
			start_title.SetActiveRecursively(false);
		}
	}
	
	void gotoMeet() {
		state= STATE.MEET;	
		playerWins=0;
		playerLoses=0;
		generation=1;
		
		if(playerAlien == null) {
			GameObject playerAlienObj= AlienMating.spawnAlien(0);	
			playerAlien= playerAlienObj.GetComponent<AlienGenome>();
		}
		{
			GameObject alienObj= AlienMating.spawnAlien(0);	
			alienObj.SetActiveRecursively(false);
			enemyAliens.Add(alienObj.GetComponent<AlienGenome>());
		}
		{
			GameObject alienObj= AlienMating.spawnAlien(1);	
			alienObj.SetActiveRecursively(false);
			enemyAliens.Add(alienObj.GetComponent<AlienGenome>());
		}
		{
			GameObject alienObj= AlienMating.spawnAlien(3);	
			alienObj.SetActiveRecursively(false);
			enemyAliens.Add(alienObj.GetComponent<AlienGenome>());
		}
		{
			GameObject alienObj= AlienMating.spawnAlien(4);	
			alienObj.SetActiveRecursively(false);
			enemyAliens.Add(alienObj.GetComponent<AlienGenome>());
		}
		
		meet_ui.setPlayerAlien(playerAlien);
		meet_title.SetActiveRecursively(true);
	}
	
	void meet_Done() {
		leaveMeet();
		gotoMate();
	}

	
	void leaveMeet() {
		meet_title.SetActiveRecursively(false);
	}
	
	void gotoMate() {
		state= STATE.MATE;
		mate_title.SetActiveRecursively(true);
		Debug.Log ("gotoMates: fightDifficulty " + fightDifficulty);
		mate_ui.startMating(playerAlien, fightDifficulty, 1, enemyAliens[fightEnemy]);
		generation++;
		audio.play(playerAlien);
	}
	
	void mate_OffspringConfirmed() {
		Debug.Log("mate_OffspringConfirmed");
		Destroy(playerAlien.gameObject);
		
		GameObject obj= AlienMating.duplicateAlien(mate_ui.finalOffspring.target);
		playerAlien= obj.GetComponent<AlienGenome>();
		playerAlien.Generation= generation;
		obj.SetActiveRecursively(false);
		
		if(audio != null)
			audio.play(playerAlien);
	}
	
	void mate_Done() {
		Debug.Log("mate_Done");
		leaveMate();	
		gotoFight();
	}
	
	void leaveMate() {
		mate_title.SetActiveRecursively(false);	
		
		
		mate_ui.destroyAll();
	}
	
	
	void gotoFight() {
		state= STATE.FIGHT;
		fight_title.SetActiveRecursively(true);
		fight_ui.StartFight(playerAlien, enemyAliens[fightEnemy]);
		playerAlien.gameObject.SetActiveRecursively(false);
		audio.play(AudioResource.STATE.BATTLE);
	}
	
	void fight_Finished() {
		if( fight_ui.playerScore.score > fight_ui.enemyScore.score ) {
			fightEnemy++;	
			fightDifficulty++;
			
			playerWins++;
			
			audio.playOnce(AudioResource.STATE.VICTORY);
		}
		else {
			audio.playOnce(AudioResource.STATE.LOSS);
			playerLoses++;
		}
	}
	
	void fight_Done() {
		
				
		gotoLeaveFight();
		
		if( fightEnemy < enemyAliens.Count)
			gotoMate();
		else
			gotoFinal();
	}
	
	void gotoLeaveFight() {
		fight_title.SetActiveRecursively(false);
		//fight_ui.releaseFinalStage();
	}
	
	void gotoFinal() {
		final_title.SetActiveRecursively(true);
		final_ui.setPlayerAlien(playerAlien);		
		final_ui.wins= playerWins;
		final_ui.loses= playerLoses;
		final_ui.generations= playerAlien.Generation;
	}
	
}
